package net.sxnic.item.action;

import java.util.List;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.action.MdataAction;
import net.sxnic.item.zzdw.ZzdwManager;
import net.sxnic.jhitem.itemoperation.ItemOperation;
import net.sxnic.normal.util.Constants;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.User;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.task.TaskManager;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 除了申报单位和管理员之外，其他用户的审批列表
 * @author 孙宇飞
 *
 */
@SuppressWarnings("serial")
public class VerifyList extends MdataAction {

	private Page datas;

	@Autowired
	private TaskManager taskManager;
	
	@Autowired
	private ZzdwManager zzdwManager;

	private String txtName;
	
	private String zzdw;
	//计划类别
	private String soperCode;
	//计划类别实体列表
	private List<ItemOperation> operationList;
	
	// zzdw gldw gzdw
	private String deptCate;
		
	public String execute() throws Exception {
		//年份处理，若属性表中未定义，则读取当前年
		String currentYear = PropertyUtil.getProperty("item.currentyear");
		if(StringUtils.isBlank(currentYear)) {
			currentYear = ItemConstants.APP_YEAR;
		}
		//操作年，只有操作年=申报书里的年份，才能提交修改，否则只能查看
		request.setAttribute("optYear", this.getJhYear(currentYear));
		
		if(StringUtils.isBlank(appYear)){
			appYear= currentYear;
		}
		catalogAlias = ItemConstants.SYSTEM_CALALOG_ALIAS;
		
		//组织单位、管理单位、工作单位 角色才能进入此action
		
		int pageNumber = WebUtils.getPageNumber(request);
		int pageSize = WebUtils.getPageSize(request);

		User user = ucenterService.findUserByUserName(AppContext
				.getUserName(request));
		
		//deptCate = zzdwManager.findByDeptCodeCatalogAlias(user.getDept().getCode(), catalogAlias).getCate();
		
		//非申报单位的任务承担者都是 具有此taskAssignee角色的用户
		String assignee =ucenterService.findTaskAssignee(user);
		
		DetachedCriteria dc = DetachedCriteria.forClass(Task.class);		
		
		dc.add(Restrictions.eq("assignee", assignee));
		dc.add(Restrictions.in("status", new String[]{Task.TASK_STATUS_ACTIVE,Task.TASK_STATUS_ENDED}));
		dc.add(Restrictions.eq("lastOne", true));
		
		dc.createAlias("mdata", "mdata");		
		if(StringUtils.isNotBlank(appYear)){
			dc.add(Restrictions.eq("mdata.appYear", this.getJhYear(appYear)));			
		}
		
		if(StringUtils.isNotBlank(catalogAlias)){
			dc.add(Restrictions.eq("mdata.catalog1", catalogAlias));
		}
		
		if(StringUtils.isNotBlank(catalogCode)){
			dc.add(Restrictions.eq("mdata.catalog2", catalogCode));
		}
		
		if(StringUtils.isNotBlank(zzdw)){
			dc.add(Restrictions.eq("mdata.zzdwCode", zzdw));
		}
		
		if(StringUtils.isNotBlank(txtName)){
			dc.add(Restrictions.like("mdata.appName", "%"+txtName+"%"));
		}

		if(StringUtils.isNotBlank(soperCode)){
			dc.add(Restrictions.eq("mdata.soperCode", soperCode));
		}
		//如果用户的角色为管理单位，就要对soperCode进行筛选
		if(ucenterService.isUserInRole(user, "gldw"))
		{
			operationList = operationManager.findSoperCode(ItemConstants.SYSTEM_CALALOG_ALIAS, "001", user.getDept().getCode());
			if(StringUtils.isNotBlank(findSoperCode(operationList)))
			{
				dc.add(Restrictions.in("mdata.soperCode", findSoperCode(operationList).split(Constants.SPLIT_COMMA)));
			}
		}
		
		datas = taskManager.getPageByDetachedCriteria(pageNumber, pageSize, dc);				

		return SUCCESS;
	}
	
	private String findSoperCode(List<ItemOperation> flist)
	{
		String temp = "";
		for(ItemOperation itemOperation : flist)
		{
			temp = temp + itemOperation.getSoperCode()+",";
		}
		if(StringUtils.isNotBlank(temp))
		{
			StringUtils.removeEnd(temp, Constants.SPLIT_COMMA);
		}
		return temp;
	}

	public Page getDatas() {
		return datas;
	}

	public void setDatas(Page datas) {
		this.datas = datas;
	}

	public String getTxtName() {
		return txtName;
	}

	public void setTxtName(String txtName) {
		this.txtName = txtName;
	}

	public String getZzdw() {
		return zzdw;
	}

	public void setZzdw(String zzdw) {
		this.zzdw = zzdw;
	}

	public String getDeptCate() {
		return deptCate;
	}

	public void setDeptCate(String deptCate) {
		this.deptCate = deptCate;
	}

	public String getSoperCode() {
		return soperCode;
	}

	public void setSoperCode(String soperCode) {
		this.soperCode = soperCode;
	}

	public List<ItemOperation> getOperationList() {
		return operationList;
	}

	public void setOperationList(List<ItemOperation> operationList) {
		this.operationList = operationList;
	}
}
